package com.o2opays.wechat.controller;

import com.o2opays.wechat.interFaceBean.LoginByPawPar;
import com.o2opays.wechat.pojo.*;
import com.o2opays.wechat.service.LoginService;
import com.o2opays.wechat.service.TranInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.logging.Logger;

/**
 * Created by su on 2017/12/3 0003 15:44
 * 619389279@qq.com
 */
@RestController
@RequestMapping("server")
public class ServerController {

    Logger logger = Logger.getLogger("ServerController");

    @Autowired
    LoginService loginService;

    @Autowired
    TranInfoService tranInfoService;

    @RequestMapping("loginByPwd")
    public RepMess loginByPwd(LoginByPawPar pawPar, HttpSession httpSession) {
        RepMess mess = new RepMess();
        String openId = (String) httpSession.getAttribute("openId");
        String pwd = pawPar.getPwd();
        String userName = pawPar.getUserName();
        HttpResult result = loginService.login(openId, pwd, userName);
        MerchatUserInfo info = (MerchatUserInfo) result.getData();
        mess.setResult(info);
        mess.setErrcode(result.getCode());
        mess.setErrmsg(result.getMsg());
        if ("00000".equals(result.getCode())) {
            httpSession.setAttribute("merchantInfo", result.getData());
        }
        return mess;
    }

    @RequestMapping("getTranList")
    public RepMess getTranList(TranListReuest listReuest, HttpSession httpSession) {
        MerchatUserInfo userInfo = (MerchatUserInfo) httpSession.getAttribute("merchantInfo");
        RepMess mess = new RepMess();
        if (userInfo != null) {
            listReuest.setMrchId(userInfo.getMrchId());
            HttpResult result = tranInfoService.getTranInfoList(listReuest);
            mess.setErrmsg(result.getMsg());
            mess.setErrcode(result.getCode());
            mess.setResult(result.getData());
        } else {
            mess.setErrmsg("登录过期");
            mess.setErrcode("99999");
        }
        return mess;
    }

    @RequestMapping("registeronLine")
    public RepMess registeronLine(TranRegisterRequest registerRequest,HttpSession httpSession){
        MerchatUserInfo userInfo = (MerchatUserInfo) httpSession.getAttribute("merchantInfo");
        RepMess mess = new RepMess();
        if (userInfo !=null){
            registerRequest.setMrchId(userInfo.getMrchId());
            HttpResult result = tranInfoService.getRegisteronLineByMrchId(registerRequest);
            mess.setErrmsg(result.getMsg());
            mess.setErrcode(result.getCode());
            mess.setResult(result.getData());

        }else{
            mess.setErrmsg("登录过期");
            mess.setErrcode("99999");
        }
        return mess;
    }

}
